<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改课程管理')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-course-edit" th:object="${ccCourse}">
        <input name="id" th:field="*{id}" type="hidden">
        <!--        <div class="form-group">-->
        <!--            <label class="col-sm-3 control-label">选择分类：</label>-->
        <!--            <div class="col-sm-8">-->
        <!--                <label class="checkbox-inline" th:each="sub:${subjectList}" th:object="${sub}">-->
        <!--                    <input type="checkbox" name="subjectIds" th:value="*{id}" th:text="*{subjectName}">-->
        <!--                </label>-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label">选择分类：</label>
            <div class="col-sm-8">
                <select id="subjectIds" class=" form-control select2-multiple" multiple>
                    <th:block th:each="sub:${subjectList}" th:object="${sub}">
                        <option th:value="*{id}" th:text="*{subjectName}"></option>
                    </th:block>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程标题：</label>
            <div class="col-sm-8">
                <input name="courseSubtitle" th:field="*{courseName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程副标题：</label>
            <div class="col-sm-8">
                <input name="courseSubtitle" th:field="*{courseSubtitle}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程简介：</label>
            <div class="col-sm-8">
                <input type="hidden" class="form-control" th:field="*{courseIntro}">
                <div class="summernote" id="courseIntro"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程难度：</label>
            <div class="col-sm-8">
                <select name="courseDifficulty" class="form-control m-b"
                        th:with="type=${@dict.getType('course_difficulty')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{courseDifficulty}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否打折：</label>
            <div class="col-sm-2">
                <div class="radio-box" id="ifDiscont" th:each="dict : ${@dict.getType('course_if_discount')}">
                    <input type="radio"
                           th:id="${'courseIfDicount_' + dict.dictCode}" name="courseIfDicount"
                           th:value="${dict.dictValue}" th:field="*{courseIfDicount}">
                    <label th:for="${'courseIfDicount_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
            <label class="col-sm-1 control-label">原价：</label>
            <div class="col-sm-1">
                <input name="courseOriginalPrice" th:field="*{courseOriginalPrice}"
                       class="form-control" type="text">
            </div>

            <label class="col-sm-1 control-label">折后价：</label>
            <div class="col-sm-1">
                <input name="courseDicountPrice" th:field="*{courseDicountPrice}"
                       class="form-control" type="text">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">打折描述：</label>
            <div class="col-sm-8">
                <textarea name="courseDiscountRemark" class="form-control">[[*{courseDiscountRemark}]]</textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">排序：</label>
            <div class="col-sm-8">
                <input name="courseSort" th:field="*{courseSort}" class="form-control" type="text">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否轮播：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'courseIfShuffling_' + dict.dictCode}" name="courseIfShuffling"
                           th:value="${dict.dictValue}" th:field="*{courseIfShuffling}">
                    <label th:for="${'courseIfShuffling_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否精品推荐：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'courseIfBoutique_' + dict.dictCode}" name="courseIfBoutique"
                           th:value="${dict.dictValue}" th:field="*{courseIfBoutique}">
                    <label th:for="${'courseIfBoutique_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否免费：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('course_if_free')}">
                    <input type="radio" th:id="${'courseIfFree_' + dict.dictCode}" name="courseIfFree"
                           th:value="${dict.dictValue}" th:field="*{courseIfFree}">
                    <label th:for="${'courseIfFree_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否对外：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('course_if_foreign')}">
                    <input type="radio" th:id="${'courseIfForeign_' + dict.dictCode}" name="courseIfForeign"
                           th:value="${dict.dictValue}" th:field="*{courseIfForeign}">
                    <label th:for="${'courseIfForeign_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">封面：</label>
            <div class="col-sm-8">
                <input type="hidden" name="courseCover" th:field="*{courseCover}">
                <div class="file-loading">
                    <input class="form-control file-upload" id="courseCover" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">轮播图：</label>
            <div class="col-sm-8">
                <input type="hidden" name="courseShuffling" th:field="*{courseShuffling}">
                <div class="file-loading">
                    <input class="form-control file-upload" id="courseShuffling" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">pc端轮播图：</label>
            <div class="col-sm-8">
                <input type="hidden" name="coursePcShuffling" th:field="*{coursePcShuffling}">
                <div class="file-loading">
                    <input class="form-control file-upload" id="coursePcShuffling" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否展示详情：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'courseIfDetail_' + dict.dictCode}" name="courseIfDetail"
                           th:value="${dict.dictValue}" th:field="*{courseIfDetail}">
                    <label th:for="${'courseIfDetail_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">播放量：</label>
            <div class="col-sm-8">
                <input name="courseViewCount" th:field="*{courseViewCount}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">上线状态：</label>
            <div class="col-sm-8">
                <select name="courseOnlineState" class="form-control m-b"
                        th:with="type=${@dict.getType('course_online_status')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{courseOnlineState}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="remark" class="form-control">[[*{remark}]]</textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<script th:inline="javascript">
    var prefix = ctx + "course/course";
    //页面加载完毕后将课程对应的专题渲染上去
    var subIds = [[${subIds}]];
    var subjectIds = []
    for (let i = 0; i < subIds.length; i++) {
        subjectIds.push(subIds[i])
    }
    $("#subjectIds").val(subjectIds);

    //获取多选框的值
    $('#subjectIds').change(function (e) {
        let o = document.getElementById('subjectIds').getElementsByTagName('option');
        subjectIds = []
        for (let i = 0; i < o.length; i++) {
            if (o[i].selected) {
                subjectIds.push(o[i].value)
            }
        }
    })

    $("#form-course-edit").validate({
        focusCleanup: true
    });

    //监听是否打折
    console.log($("[name=courseIfDicount]").next())
    $("#ifDiscont").click(function () {
            console.log(123)
        }
    )

    function submitHandler() {
        if ($.validate.form()) {
            edit()
        }
    }

    function edit() {
        var formData = new FormData();
        formData.append('id', $("[name=id]").val())
        formData.append('subjectIds', subjectIds.join(","));
        formData.append('courseName', $("[name=courseName]").val());
        formData.append('courseSubtitle', $("[name=courseSubtitle]").val());
        formData.append('courseIntro', $("[name=courseIntro]").val());
        formData.append('courseDifficulty', $("[name=courseDifficulty]").find("option:selected").val());
        formData.append('courseIfDicount', $("[name=courseIfDicount]:checked").val());
        formData.append('courseDiscountRemark', $("[name=courseDiscountRemark]").val());
        formData.append('courseOriginalPrice', $("[name=courseOriginalPrice]").val());
        formData.append('courseSort', $("[name=courseSort]").val());
        formData.append('courseDicountPrice', $("[name=courseDicountPrice]").val());
        formData.append('courseIfShuffling', $("[name=courseIfShuffling]:checked").val());
        formData.append('courseIfBoutique', $("[name=courseIfBoutique]:checked").val());
        formData.append('courseIfFree', $("[name=courseIfFree]:checked").val());
        formData.append('courseIfForeign', $("[name=courseIfForeign]:checked").val());
        formData.append('courseIfDetail', $("[name=courseIfDetail]:checked").val());
        formData.append('courseViewCount', $("[name=courseViewCount]").val());
        formData.append('courseOnlineState', $("[name=courseOnlineState]").find("option:selected").val());
        formData.append('remark', $("[name=remark]").val());
        if ($('[id=courseCover][name=file]')[0].files[0] != undefined) {
            formData.append('courseCoverFile', $('[id=courseCover][name=file]')[0].files[0]);
        }
        if ($('[id=courseShuffling][name=file]')[0].files[0] != undefined) {
            formData.append('courseShufflingFile', $('[id=courseShuffling][name=file]')[0].files[0]);
        }
        if ($('[id=coursePcShuffling][name=file]')[0].files[0] != undefined) {
            formData.append('coursePcShufflingFile', $('[id=coursePcShuffling][name=file]')[0].files[0]);
        }
        $.ajax({
            url: prefix + "/edit",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });
    }

    $(".file-upload").each(function (i) {
        var val = $("input[name='" + this.id + "']").val()
        $(this).fileinput({
            initialPreviewAsData: true,
            initialPreview: [val],
            maxFileCount: 1,
            autoReplace: true,
            showUpload: false, //是否显示上传按钮
            dropZoneTitle: "拖拽文件到这里 …" + `<br>` + "仅支持单文件上传",
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })
        $(this).fileinput('_initFileActions');
    });

    $(function () {
        $('.summernote').each(function (i) {
            $('#' + this.id).summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                callbacks: {
                    onChange: function (contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function (files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function (result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function (error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            var content = $("input[name='" + this.id + "']").val();
            $('#' + this.id).summernote('code', content);
        })
    });
</script>
</body>
</html>